#!/bin/bash

# ----------------------------------------------------------------------
# Filename:   ftp_start.sh
# Version:    1.0
# Date:       2013/06/24
# Author:     huijing.hei
# Email:      huijing.hei@cs2c.com.cn
# Summary:    02系统基本功能-04网络服务-01vsftp-01字符模式-01服务控制
# Notes:       ***
# Copyright:    China Standard Software Co., Ltd.
# History：     
#             Version 1.0, 2013/06/24
#             - Start ftp service.
# ----------------------------------------------------------------------

##! @TODO: start&stop ftp service
##! @AUTHOR: huijing.hei
##! @VERSION: 1.0 
##! @OUT: 0 => success; 1 => failure

function do_test()
{
    serv=vsftpd

    echo "${serv} start case"

    # restart
    ${RSH} -l root $RHOST \
       "command -v systemctl && /bin/systemctl restart ${serv}.service || /sbin/service ${serv} restart"

    RE=`${RSH} -l root $RHOST \
       "/bin/netstat -ntlp | grep ${serv}"`
    echo "${RE}" | grep ${serv}
    EchoResult "service ${serv} restart test."

    # stop
    ${RSH} -l root $RHOST \
       "command -v systemctl && /bin/systemctl stop ${serv}.service || /sbin/service ${serv} stop"

    RE=`${RSH} -l root $RHOST \
       "/bin/netstat -ntlp | grep ${serv}"`
    ! echo "${RE}" | grep ${serv}
    EchoResult "service ${serv} stop test."

    # start
    ${RSH} -l root $RHOST \
       "command -v systemctl && /bin/systemctl start ${serv}.service || /sbin/service ${serv} start"

    RE=`${RSH} -l root $RHOST \
       "/bin/netstat -ntlp | grep ${serv}"`
    echo "${RE}" | grep ${serv}
    EchoResult "service ${serv} start test."

    trap do_cleanup INT EXIT

}

function do_cleanup
{
    # stop
    ${RSH} -l root $RHOST \
       "command -v systemctl && /bin/systemctl stop ${serv}.service || /sbin/service ${serv} stop"
}


# include lib files

if [ -z "$SFROOT" ]
then
    CMDLIB=./
    SFROOT=./
else
    CMDLIB=${SFROOT}/testcases/Network_Service
fi

. ${SFROOT}/lib/Echo.sh
. ${CMDLIB}/lib/Net.sh
. ${SFROOT}/lib/XmlParse.sh

. ${CMDLIB}/setup.sh

# get RHOST, RUSERNAME, PASSWORD from config.xml
DATA_FILE="${SFROOT}/testcases/Network_Service/config.xml"
[ -e $DATA_FILE ] || { echo "$DATA_FILE not find, test end."; exit 1; }

XmlParse ${DATA_FILE}

GetValue REHOST ReHost
GetValue RUSERNAME UserName
GetValue PASSWORD RootPasswd

export RHOST=${REHOST}
export RUSERNAME=${RUSERNAME}
export PASSWORD=${PASSWORD}


setup_env || { echo "setup env failed, test end."; exit 1; }

do_test
EchoResult "testcase: ${serv} service start, stop, restart"
